// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// s [author] rhyme_lph
// j [email]  rhymelph@gmail.com
// T [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/fixed_asset_entity.dart';
import 'package:finance_app/src/provider/all_reports/fixed_asset/fixed_asset_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/select/normal_radio.dart';
import 'package:flutter/material.dart';

export 'package:finance_app/src/entities/account_entity.dart';

@RouterMeta(paramName: "fixedAssetPage")
class FixedAssetPage extends StatefulWidget {
  final FixedAssetEntity select;

  const FixedAssetPage({Key key, this.select}) : super(key: key);

  @override
  _FixedAssetPageState createState() => _FixedAssetPageState();
}

class _FixedAssetPageState extends State<FixedAssetPage>
    with PagePresenter<FixedAssetPage, FixedAssetProvider> {
  @override
  void initState() {
    super.initState();
    provider.currentSelect = widget.select;
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.fixed_asset,
      );

  @override
  Widget get buildWidget => listBuilder<int>(
      selector: (_, __) => __.data.length,
      builder: (BuildContext context, int value, Widget child) =>
          ListView.builder(
            padding: EdgeInsets.symmetric(
              horizontal: getDp(35),
              vertical: getDp(25),
            ),
            itemBuilder: _buildItems,
            itemCount: value,
          ));

  Widget _buildItems(BuildContext context, int index) {
    int _index = index;
    return listBuilder<FixedAssetEntity>(
      selector: (_, __) => __.data[_index],
      builder: (BuildContext context, FixedAssetEntity value, Widget child) =>
          PressedOpacityButton(
        onPressed: () => provider.onChangeFixedAsset(value),
        padding: EdgeInsets.symmetric(
          vertical: getDp(14),
        ),
        child: NormalRadio(
          title: '${value.name}',
          isSelect: value?.id == provider.currentSelect?.id,
        ),
      ),
    );
  }
}
